package com.glodon.paas.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.MessageDigest;

public class GenericsUtil {
	@SuppressWarnings("all")
	public static Class getClazz(Class clazz) {

		Type type = clazz.getGenericSuperclass();
		if (!(type instanceof ParameterizedType)) {
			throw new RuntimeException();
		}

		ParameterizedType pt = (ParameterizedType) type;

		return (Class) pt.getActualTypeArguments()[0];
	}

	public static String md5(String input) {
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(input.getBytes("UTF-8"));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		byte[] digest = md.digest();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < digest.length; i++) {
			String hex = Integer.toHexString(digest[i] & 0xff).length() == 2 ? Integer
					.toHexString(digest[i] & 0xff) : 0 + Integer
					.toHexString(digest[i] & 0xff);
			sb.append(hex);
		}
		return sb.toString().toUpperCase();
	}

}
